package com.joe.utils;

import java.util.*;

/**
 * Created by joe on 2019/1/17
 */
public class New {

    public static <T> List<T> makeList() {
        return new ArrayList<>();
    }

    public static <T> List<T> makeList(T... args) {
        return new ArrayList<>(Arrays.asList(args));
    }

    public static <T> Set<T> makeSet() {
        return new HashSet<>();
    }

    public static <T> Set<T> makeSet(T... args) {
        return new HashSet<>(Arrays.asList(args));
    }

    public static <K, V> Map<K, V> makeMap() {
        return new HashMap<>();
    }

    public static <K, V> Map<K, V> makeMap(K[] keys, V[] values) {
        if (keys.length != values.length) {
            throw new RuntimeException("key's length is not equals value's!");
        }
        Map<K, V> maps = new HashMap<>();
        for (int i = 0; i < keys.length; i++) {
            maps.put(keys[i], values[i]);
        }
        return maps;
    }

    public static Collection<?> newCollection(Class<?> colleClass) throws IllegalAccessException, InstantiationException {
        if (colleClass.isInterface()) {
            String simpleName = colleClass.getSimpleName();
            switch (simpleName) {
                case "List":
                    return new ArrayList<>();
                case "Set":
                    return new HashSet<>();
            }
        }
        return (Collection<?>) colleClass.newInstance();
    }


}
